import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { isHttp, isEmpty } from '@/utils/validate'
import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import defAva from '@/static/images/profile.png'

const imgUrl = config.imgUrl

const user = {
  state: {
    token: getToken(),
    id: storage.get(constant.id),
    name: storage.get(constant.name),
    nickname: storage.get(constant.nickname),
    email: storage.get(constant.email),
    promoCode: storage.get(constant.promoCode),
    avatar: storage.get(constant.avatar),
    roles: storage.get(constant.roles),
    permissions: storage.get(constant.permissions),
    password: storage.get(constant.password)
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_ID: (state, id) => {
      state.id = id
      storage.set(constant.id, id)
    },
    SET_NAME: (state, name) => {
      state.name = name
      storage.set(constant.name, name)
    },
    SET_NICKNAME: (state, nickname) => {
      state.nickname = nickname
      storage.set(constant.nickname, nickname)
    },
    SET_PROMOCode: (state, promoCode) => {
      state.promoCode = promoCode
      storage.set(constant.promoCode, promoCode)
    },
    SET_PASSWORD: (state, password) => {
      state.password = password
      storage.set(constant.password, password)
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
      storage.set(constant.avatar, avatar)
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
      storage.set(constant.roles, roles)
    },
    SET_EMAIL: (state, email) => {
      state.email = email
      storage.set(constant.email, email)
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
      storage.set(constant.permissions, permissions)
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      return new Promise((resolve, reject) => {
        login(username, password)
          .then((res) => {
            let { access_token } = res.result
            setToken(access_token)
            commit('SET_TOKEN', access_token)
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo()
          .then((res) => {
            let { data } = res.result
            const user = data
            let avatar = user.headPortrait || ''
            if (!isHttp(avatar)) {
              avatar = isEmpty(avatar) ? defAva : imgUrl + avatar
            }
            const userid = isEmpty(user) || isEmpty(user.id) ? '' : user.id
            const username = isEmpty(user) || isEmpty(user.username) ? '' : user.username
            const nickname = isEmpty(user) || isEmpty(user.nickname) ? '' : user.nickname
            const userEmail = isEmpty(user) || isEmpty(user.email) ? '' : user.email
            const userPromoCode = isEmpty(user) || isEmpty(user.promoCode) ? '' : user.promoCode
            const userPassword = isEmpty(user) || isEmpty(user.cashPaswword) ? '' : user.cashPaswword
            console.log('userPassword', userPassword)
            if (res.roles && res.roles.length > 0) {
              commit('SET_ROLES', res.roles)
              commit('SET_PERMISSIONS', res.permissions)
            } else {
              commit('SET_ROLES', ['ROLE_DEFAULT'])
            }
            commit('SET_ID', userid)
            commit('SET_NAME', username)
            commit('SET_EMAIL', userEmail)
            commit('SET_NICKNAME', nickname)
            commit('SET_PROMOCode', userPromoCode)
            commit('SET_PASSWORD', userPassword)
            commit('SET_AVATAR', avatar)
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          removeToken()
          storage.clean()
          resolve()
        }, 500)
        return
        logout(state.token)
          .then(() => {
            commit('SET_TOKEN', '')
            commit('SET_ROLES', [])
            commit('SET_PERMISSIONS', [])
            removeToken()
            storage.clean()
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    }
  }
}

export default user
